package com.example.administrator.managesystem.activity.firstpage.circle.ui;

import android.content.Context;
import android.util.Log;

import com.example.administrator.managesystem.bean.PsdSettingBean;
import com.example.administrator.managesystem.bean.SendCircleBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.ProfileView;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CirclePresenter  {
    private CircleView circleView;

    public CirclePresenter(CircleView circleView) {
        this.circleView = circleView;
    }

    public void uploadLand(String user_id, String token, String content, String address, List<String> list, final Context context){

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
                //.addFormDataPart(ParamKey.TOKEN, token);//ParamKey.TOKEN 自定义参数key常量类，即参数名
        //多张图片
        for (int i = 0; i < list.size(); i++) {
            File file = new File(list.get(i));//filePath 图片地址
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("images", file.getName(), imageBody);//"imgfile"+i 后台接收图片流的参数名
        }
        List<MultipartBody.Part> parts = builder.build().parts();

        ServiceManager.getInstance(context).getService(GuideAPI.class).MySendRequest(user_id,token,content,address,parts)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<SendCircleBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                        circleView.showMsg();
                    }

                    @Override
                    public void onNext(SendCircleBean sendCircleBean) {
                      if(sendCircleBean.getStatus()==200){
                          ToastUtils.showToast(context,sendCircleBean.getData());
                          circleView.navigateToHome();
                          circleView.showMsg();
                      }else{
                          ToastUtils.showToast(context,sendCircleBean.getMsg());
                          circleView.showMsg();
                      }

                    }

                });

    }
}
